package b.pay.impl

import java.net.URI

import akka.event.slf4j.SLF4JLogging
import com.lightbend.lagom.scaladsl.api.Descriptor.Call
import com.lightbend.lagom.scaladsl.api.ServiceLocator
import com.lightbend.lagom.scaladsl.api.ServiceSupport.ScalaMethodServiceCall
import com.lightbend.lagom.scaladsl.devmode.LagomDevModeComponents

import scala.concurrent.{ExecutionContext, Future}

/**
  * 贲国称 于 2017/2/8 创建。
  */
class C第三方支付ServiceLocator4Dev(t: LagomDevModeComponents, d: (String, Call[_, _]) => Future[Option[URI]]) extends ServiceLocator with SLF4JLogging {
  log.error("MyServiceLocator object")
  override def locate[Request](name: String, serviceCall: Call[Request, _], request: Request): Future[Option[URI]] = {
    log.error(s"locate-nm: $name ${serviceCall.callId}")
    log.error(s"locate-rq: $request")
    log.error(s"locate-cl: ${serviceCall.serviceCallHolder.asInstanceOf[ScalaMethodServiceCall[_, _]].method}")
    d(request.asInstanceOf[String], serviceCall)
  }
  override def doWithService[T, Request](name: String, serviceCall: Call[Request, _], request: Request)(block: (URI) => Future[T])(implicit ec: ExecutionContext): Future[Option[T]] = {
    locate(name, serviceCall, request).flatMap {
      case (Some(uri)) => block(uri).map(Some.apply)
      case None        => Future.successful(None)
    }
  }
}
